package xyz.robinbingo.effectivejava.chapter02.item06;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import cn.hutool.core.lang.Console;

import java.util.regex.Pattern;

// Reusing expensive object for improved performance
public class RomanNumerals {
    private static final Pattern ROMAN = Pattern.compile("^(?=.)M*(C[MD]|D?C{0,3})" + "(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$");

    static boolean isRomanNumeral(String s) {
        TimeInterval timer = DateUtil.timer();
        boolean matches = ROMAN.matcher(s).matches();
        Console.log("1:{}", timer.interval());
        return matches;
    }

    // Performance can be greatly improved!
    static boolean isRomanNumeral1(String s) {
        TimeInterval timer = DateUtil.timer();
        boolean matches = s.matches("^(?=.)M*(C[MD]|D?C{0,3})" + "(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})$");
        Console.log("2:{}", timer.interval());
        return matches;
    }
}